$(function(){
		openPwd();

        $('#editpass').click(function() {
            $('#w').window('open');
        });

        $('#okButton').click(function() {
            serverLogin();
        })

		$('#btnCancel').click(function(){closePwd();})

        $('#loginOut').click(function() {
            $.messager.confirm('提示', '您确定要退出本次登录吗?', function(r) {

                if (r) {
                    location.href = '/logout';
                }
            });
        })
        init();
        //$('#window-mmask').remove();
        tabClose();
    	tabCloseEven();
    	//取登录用户信息
	    $.ajax({
			type: "post",
			contentType:"application/json",
			url: "/authenticated/user/sessionuser",
			dataType:"json",
			success: function(result){
				if(result.success){
					sessionuser=result.data;
					$("#nickname").html(sessionuser.nickname);
				}	
			},
			error: function(){
				alert("error");
			}
		});
	    addTab("首页","/view/asset/home.html",null);
	});
	var _menus = {};
	
        //设置登录窗口
    function openPwd() {
        $('#w').window({
            title: '修改密码',
            width: 300,
            modal: true,
            shadow: true,
            closed: true,
            height: 200,
            resizable:false
        });
    }
    //关闭登录窗口
    function closePwd() {
        $('#w').window('close');
    }

		        
    function stringToHex(str){
		var val = "";
		for (var i = 0; i < str.length; i++) {
			val += str.charCodeAt(i).toString(16);
		}
		return val;
	}
        //修改密码
        function serverLogin() {
            var newpass = $('#txtNewPass').val();
            var rePass = $('#txtRePass').val();
            var txtPass= $('#txtPass').val();
            if (txtPass == '') {
                msgShow('提示', '请输入密码！', 'warning');
                return false;
            }
            if (newpass == '') {
                msgShow('提示', '请输入新密码！', 'warning');
                return false;
            }
            if (rePass == '') {
                msgShow('提示', '请再一次输入新密码！', 'warning');
                return false;
            }

            if (newpass != rePass) {
                msgShow('提示', '两次密码不一至！请重新输入', 'warning');
                return false;
            }
            var salt="";
            var encrypwd = CryptoJS.MD5(CryptoJS.enc.Hex.parse(stringToHex(txtPass)+salt)).toString();
    		var newencrypwd = CryptoJS.MD5(CryptoJS.enc.Hex.parse(stringToHex(newpass)+salt)).toString();
    		var pwdson = {};
    		pwdson.password=encrypwd;
    		pwdson.newpassword=newencrypwd;
            $._post("/authenticated/ajax/update/update_password",JSON.stringify(pwdson), function(result) {
            	if(result.success){
            		msgShow('提示', '密码修改成功！' , 'info');
            	}else{
            		msgShow('提示', '请求失败！<br>'+result.message , 'info');
            	}
            	$('#txtNewPass').val('');
                $('#txtRePass').val('');
                $('#txtPass').val('');
                closePwd();
            });
        }

		        var onlyOpenTitle="首页";//不允许关闭的标签的标题
		        
		        function init(){
		        	$.ajax({
						type: "post",
						url: "/authenticated/ajax/select_menu",
						data:JSON.stringify({"menu_status":"enabled"}),
						dataType:"json",
						contentType:"application/json",
						success: function(result){
							if(result.success){
								_menus=transformCombotreeArray(result.data);
								InitLeftMenu();
							}else{
								$.messager.alert("提示", "初始化菜单失败！");
							}
						}
					});
		        }
		        
		        
		        
		        //初始化左侧
		        
		        var menuField={
		        		id:"id",
		        		name:"name",
		        		path:"path",
		        		icon:"icon"
		        };
		        function InitLeftMenu() {
		        	console.log(_menus);
		        	var menulist ='<div class="easyui-accordion " data-options="multiple:true" >';
		        	var selectedPanelname = '';
		            $.each(_menus, function(i, n) {
		        		menulist +=`<div data-options="title:'${n.name}'" >`;
		        		if(n.children&&n.children.length>0){
		        			menulist +=`<ul class="easyui-tree">`;
		        			$.each(n.children, function(j, o) {
			        			menulist += `<li><span>${o.name}</span></li>`;
			                })
		        			menulist+='</ul>';
		        		}
		        		menulist+='</div>';
		            });
		            menulist+='</div>';
		            $("#menu").html(menulist);
		            $.parser.parse('#west');
		        }
		        function InitLeftMenu1() {
		        	//console.log(_menus);
		        	var menulist =`<ul class="layui-nav layui-nav-tree">`;
		        	var selectedPanelname = '';
		            $.each(_menus, function(i, n) {
		        		menulist +=`<li class="layui-nav-item layui-nav-itemed" ><a id="1" onClick="tabMenu(this)" class="class1" href="javascript:;" style="font-size:12px;background-color: #F2F2F2;">${n.name}<span class="layui-nav-more"></span></a>`;
		        		if(n.children&&n.children.length>0){
		        			menulist +=`<dl class="layui-nav-child">`;
		        			$.each(n.children, function(j, o) {
			        			menulist += `<dd><a onClick="addTab('${o.name}','${o.path}',this)" data-url="${o.path}" href="javascript:;" style="">${o.name}</a></dd>`;
			                })
		        			menulist+='</dl>';
		        		}
		        		menulist+='</li>';
		            });
		            menulist += '</ul>';
		            $("#menu").html(menulist);
		            $.parser.parse('#menu');
		            tabMenu();
		        }
		        function tabMenu(this_){
	        		$(this_).parent().toggleClass("layui-nav-itemed");
		        }
		        function InitLeftMenu2() {
		        	$("#menu").accordion({animate:false,fit:true,border:false});
		        	var selectedPanelname = '';
		            $.each(_menus, function(i, n) {
		        		var menulist ='';
		        		menulist +='<ul class="navlist">';
		                $.each(n.children, function(j, o) {
		        			menulist += '<li><div ><a ref="'+o.id+'" href="#" rel="' + o.path + '" ><span class="icon '+o.icon+'" >&nbsp;</span><span class="nav">' + o.name + '</span></a></div> ';
		        			if(o.children && o.children.length>0)
		        			{
		        				//li.find('div').addClass('icon-arrow');

		        				menulist += '<ul class="third_ul">';
		        				$.each(o.children,function(k,p){
		        					menulist += '<li><div><a ref="'+p.id+'" href="#" rel="' + p.path + '" ><span class="icon '+p.icon+'" >&nbsp;</span><span class="nav">' + p.name + '</span></a></div> </li>'
		        				});
		        				menulist += '</ul>';
		        			}

		        			menulist+='</li>';
		                })
		        		menulist += '</ul>';

		        		$('#menu').accordion('add', {
		                    title: n.name,
		                    bodyCls:"mpanelbody",
		                    content: menulist,
		        			border:false,
		                    iconCls: 'icon ' + n.icon
		                });

		        		if(i==0)
		        			selectedPanelname =n.name;

		            });
		        	$('#menu').accordion('select',selectedPanelname);


		        	$('.navlist li a').click(function(){
		        		var tabTitle = $(this).children('.nav').text();

		        		var url = $(this).attr("rel");
		        		var menuid = $(this).attr("ref");
		        		var icon = $(this).find('.icon').attr('class');

		        		var third = find(menuid);
		        		if(third && third.children && third.children.length>0){
		        			$('.third_ul').slideUp();

		        			var ul =$(this).parent().next();
		        			if(ul.is(":hidden"))
		        				ul.slideDown();
		        			else
		        				ul.slideUp();
		        		}else{
		        			addTab(tabTitle,url,icon);
		        			$('.navlist li div').removeClass("selected");
		        			$(this).parent().addClass("selected");
		        		}
		        	}).hover(function(){
		        		$(this).parent().addClass("hover");
		        	},function(){
		        		$(this).parent().removeClass("hover");
		        	});
		        	//选中第一个
		        	//var panels = $('#nav').accordion('panels');
		        	//var t = panels[0].panel('options').title;
		            //$('#nav').accordion('select', t);
		        }
		        //InitLeftMenu();
		        //获取左侧导航的图标
		        function getIcon(menuid){
		        	var icon = 'icon ';
		        	$.each(_menus, function(i, n) {
		        		 $.each(n.children, function(j, o) {
		        		 	if(o.id==menuid){
		        				icon += o.icon;
		        			}
		        		 })
		        	})
		        	return icon;
		        }

		        function find(menuid){
		        	var obj=null;
		        	$.each(_menus, function(i, n) {
		        		 $.each(n.children, function(j, o) {
		        		 	if(o.id==menuid){
		        				obj = o;
		        			}
		        		 });
		        	});
		        	return obj;
		        }

		        function addTab(subtitle,url,this_){
		        	if(this_){
		        		$(".menu_selected").removeClass("menu_selected");
			        	$(this_).parent().addClass("menu_selected");
		        	}
		        	if(!$('#tabs').tabs('exists',subtitle)){
		        		$('#tabs').tabs('add',{
		        			fit:true,
		        			title:subtitle,
		        			content:createFrame(url),
		        			closable:subtitle!="首页"
		        		});
		        	}else{
		        		$('#tabs').tabs('select',subtitle);
		        		$('#mm-tabupdate').click();
		        	}
		        	tabClose();
		        }

		        function createFrame(url)
		        {
		        	var s = '<iframe scrolling="no" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
		        	return s;
		        }

		        function tabClose()
		        {
		        	/*双击关闭TAB选项卡*/
		        	$(".tabs-inner").dblclick(function(){
		        		var subtitle = $(this).children(".tabs-closable").text();
		        		$('#tabs').tabs('close',subtitle);
		        	})
		        	/*为选项卡绑定右键*/
		        	$(".tabs-inner").bind('contextmenu',function(e){
		        		$('#mm').menu('show', {
		        			left: e.pageX,
		        			top: e.pageY
		        		});

		        		var subtitle =$(this).children(".tabs-closable").text();

		        		$('#mm').data("currtab",subtitle);
		        		$('#tabs').tabs('select',subtitle);
		        		return false;
		        	});
		        }


		        //绑定右键菜单事件
		        function tabCloseEven() {

		            $('#mm').menu({
		                onClick: function (item) {
		                    closeTab(item.id);
		                }
		            });

		            return false;
		        }

		        function closeTab(action)
		        {
		            var alltabs = $('#tabs').tabs('tabs');
		            var currentTab =$('#tabs').tabs('getSelected');
		        	var allTabtitle = [];
		        	$.each(alltabs,function(i,n){
		        		allTabtitle.push($(n).panel('options').title);
		        	})


		            switch (action) {
		                case "refresh":
		                    var iframe = $(currentTab.panel('options').content);
		                    var src = iframe.attr('src');
		                    $('#tabs').tabs('update', {
		                        tab: currentTab,
		                        options: {
		                            content: createFrame(src)
		                        }
		                    })
		                    break;
		                case "close":
		                    var currtab_title = currentTab.panel('options').title;
		                    if(currtab_title=='首页'){
		                    	//$.messager.alert("提示", "首页不允许关闭！");
		                    	return false;
		                    }
		                    $('#tabs').tabs('close', currtab_title);
		                    break;
		                case "closeall":
		                    $.each(allTabtitle, function (i, n) {
		                        if (n != onlyOpenTitle){
		                            $('#tabs').tabs('close', n);
		        				}
		                    });
		                    break;
		                case "closeother":
		                    var currtab_title = currentTab.panel('options').title;
		                    $.each(allTabtitle, function (i, n) {
		                        if (n != currtab_title && n != onlyOpenTitle)
		        				{
		                            $('#tabs').tabs('close', n);
		        				}
		                    });
		                    break;
		                case "closeright":
		                    var tabIndex = $('#tabs').tabs('getTabIndex', currentTab);

		                    if (tabIndex == alltabs.length - 1){
		                    	$.messager.alert("提示", "没有可以关闭的标签页！");
		                        return false;
		                    }
		                    $.each(allTabtitle, function (i, n) {
		                        if (i > tabIndex) {
		                            if (n != onlyOpenTitle){
		                                $('#tabs').tabs('close', n);
		        					}
		                        }
		                    });

		                    break;
		                case "closeleft":
		                    var tabIndex = $('#tabs').tabs('getTabIndex', currentTab);
		                    if (tabIndex == 1) {
		                    	//$.messager.alert("提示", "首页不允许关闭！");
		                        return false;
		                    }
		                    $.each(allTabtitle, function (i, n) {
		                        if (i < tabIndex) {
		                            if (n != onlyOpenTitle){
		                                $('#tabs').tabs('close', n);
		        					}
		                        }
		                    });

		                    break;
		                case "exit":
		                    $('#closeMenu').menu('hide');
		                    break;
		            }
		        }


		        //弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
		        function msgShow(title, msgString, msgType) {
		        	$.messager.alert(title, msgString, msgType);
		        }
		        
    
	

